<?php

namespace App\Console\Commands\OpenShop\Contract;

use App\Console\Commands\Traits\ExcelCommandTrait;
use App\Models\OpenShop\InvestmentPromotion\OpenShopContractTemplateModel;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class InsertContractTemplateCommand extends Command
{
    use ExcelCommandTrait;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'contractTemplate:insertBatch-data';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '批量插入加盟政策数据';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * @throws GuzzleException
     */
    public function handle()
    {
        $url = 'https://file.51kuafu.com/84/e0/c24f722f18285b8f3499fedef740a4180da481b859cfa4766e1437e88e0c.csv';

        $this->setInputExcel(false)->excelImport(InsertContractTemplateCommand::class, $url);
    }

    private array $insertBatchData = [];

    public function collection(Collection $collection, ?Command $command)
    {
        $collection->first()->map(function ($item, $row) use ($command) {
            $row = $row + 1;
            if ($row == 1) {
                return null;
            }

            // 有效期处理
            $start_time = date("Y-m-d 00:00:00");
            if ($item[12] == '永久') {
                $end_time = date("Y-m-d H:i:s", strtotime("+100 year", strtotime($start_time)));
            } else {
                $end_time = date("Y-m-d H:i:s", strtotime(str_replace('.', '-', $item[12])));
            }

            $isShow  = OpenShopContractTemplateModel::IS_SHOW_1;
            $version = 2;
            $mode    = OpenShopContractTemplateModel::MODE_3;

            // 名称
            $templateName   = trim($item[1]);
            $contractTypeV2 = [];
            if (strpos(strtolower($templateName), '新') !== false && strpos(strtolower($templateName), '裂') !== false) {
                $contractTypeV2 = [1, 2];
            } elseif (strpos(strtolower($templateName), '新') !== false) {
                $contractTypeV2 = [1];
            } elseif (strpos(strtolower($templateName), '裂') !== false) {
                $contractTypeV2 = [2];
            }

            $this->insertBatchData[] = [
                'contractType'            => $contractTypeV2[0] ?? 0,
                'templateName'            => $templateName,
                'year'                    => 3,
                // 加盟费
                'initialFee'              => $item[4] * 100,
                // 技术转让费
                'technologyTransferFee'   => $item[5] * 100,
                // 保证金
                'deposit'                 => $item[6] * 100,
                // 特权使用费第一年
                'managementCost'          => $item[7] * 100,
                // 特权使用费每年
                'managementCostEveryYear' => $item[8] * 100,
                // 装修保证金
                'decorationDeposit'       => $item[9] * 100,
                // 品牌指导费
                'guidanceFee'             => $item[10] * 100,
                // 店铺数量
                'shopCount'               => $item[11],
                // 定金金额 = 加盟费 + 品牌指导费 + 技术转让费
                'depositAmount'           => ($item[4] + $item[5] + $item[10]) * 100,
                'isShow'                  => $isShow,
                'mode'                    => $mode,
                'version'                 => $version,
                'createdAt'               => time(),
                'createdByUid'            => 0,
                // 有效期
                'startTime'               => strtotime($start_time),
                'endTime'                 => strtotime($end_time),
                'contractTypeV2'          => json_encode($contractTypeV2),
            ];

        })->filter();

        try {

            OpenShopContractTemplateModel::query()->insert($this->insertBatchData);

            echo "success:" . count($this->insertBatchData);

        } catch (Exception $e) {
            echo 'error:' . $e->getMessage();
        }

    }
}
